from django.core.paginator import Paginator
from functools import wraps
from django.utils.decorators import available_attrs
from django.http import HttpResponseRedirect


def channel_add_edit(request):
    name = request.POST.get("name")
    username = request.POST.get("username")
    tel = request.POST.get("tel")
    products = request.POST.get("products")
    sign = request.POST.get("sign")  # 展示的菜单标识
    if products:
        products = products.split(",")
    else:
        products = []
    if sign:
        sign = sign.split(",")
    else:
        sign = []
    is_show_cash = request.POST.get("show_cash")  # 是否展示佣金
    level_num = request.POST.get("level_num")  # 层级数
    banner_ad = request.POST.get("banner_ad")  # 滚动条
    check_menus = request.POST.get("check_menus")  # 检查菜单
    can_transfer = request.POST.get("can_transfer")  # 是否可以提现
    password = request.POST.get('password')  # 密码
    account = request.POST.get('account')  # 账号
    if is_show_cash and is_show_cash.isdigit() and int(is_show_cash) == 1:
        is_show_cash = True
    else:
        is_show_cash = False
    if banner_ad and banner_ad.isdigit() and int(banner_ad) == 1:
        banner_ad = True
    else:
        banner_ad = False
    if can_transfer and can_transfer.isdigit() and int(can_transfer) == 1:
        can_transfer = True
    else:
        can_transfer = False
    d = locals()
    return d


def price_add(request):
    reserved_b = request.POST.get("reserved_b", 0)
    if reserved_b.isdecimal() or reserved_b.isdigit():
        reserved_b = float(reserved_b)
    level1 = request.POST.get("level1")
    if level1.isdecimal() or level1.isdigit():
        level1 = float(level1)
    level2 = request.POST.get("level2")
    if level2.level2() or level2.isdigit():
        level2 = float(level2)
    level3 = request.POST.get("level3")
    if level3.isdecimal() or level3.isdigit():
        level3 = float(level3)
    d = locals()
    del d["request"]
    return d


class DjangoPaginator(object):
    """
    分页
    """

    def get_paging(self, data_list, page_size=10, page=1):
        page_object = Paginator(data_list, page_size)
        page_count = page_object.num_pages
        try:
            page = int(page)
        except Exception as e:
            print(e)
            page = 1
        if int(page) >= page_count:
            page = page_count
        page_info = page_object.page(page)
        now_page = page
        if page_info.has_next():
            next_page = page_info.next_page_number()
            now_page = next_page - 1
        else:
            next_page = 0
        if page_info.has_previous():
            before_page = page_info.previous_page_number()
            now_page = before_page + 1
        else:
            before_page = 0
        page_dict = {"page_count": page_count, "next_page": next_page, "before_page": before_page,
                     "now_page": now_page}
        if before_page > 1:
            before_page_2 = 1
        else:
            before_page_2 = 0
        page_dict["before_page_2"] = before_page_2
        if page_count - 1 > next_page:
            next_page_2 = next_page + 1
        else:
            next_page_2 = 0
        page_dict["next_page_2"] = next_page_2
        if hasattr(page_info, "object_list"):
            return page_info.object_list, page_dict
        else:
            return page_info, page_dict

    def make_url(self, request, page_dict):
        data = request.GET
        base_url = "?"
        for k, v in data.items():
            if k == "page":
                pass
            else:
                base_url = base_url + k + "=" + v + "&"
        page_count = page_dict.get("page_count")
        next_page = page_dict.get("next_page")
        before_page = page_dict.get("before_page")
        now_page = page_dict.get("now_page")
        before_page_2 = page_dict.get("before_page_2")
        next_page_2 = page_dict.get("next_page_2")
        if page_count:
            page_count_url = "%spage=%s" % (base_url, page_count)
        if next_page:
            next_page_url = "%spage=%s" % (base_url, next_page)
        if before_page:
            before_page_url = "%spage=%s" % (base_url, before_page)
        if now_page:
            now_page_url = "%spage=%s" % (base_url, now_page)
        if before_page_2:
            before_page_2_url = "%spage=%s" % (base_url, before_page_2)
        if next_page_2:
            next_page_2_url = "%spage=%s" % (base_url, next_page_2)
        first_page = "%spage=%s" % (base_url, "1")
        return locals()


paginator = DjangoPaginator()


def admin_login(login_url="/admin/login"):
    def decorator(view_func):
        """django.contrib.auth.decorators重写"""

        @wraps(view_func, assigned=available_attrs(view_func))
        def _wrapped_view(request, *args, **kwargs):
            path = request.get_full_path()
            # 获取账号
            if request.session.has_key("admin_salesman"):
                info = view_func(request, *args, **kwargs)
                return info
            else:
                return HttpResponseRedirect(login_url + '?next=%s' % path)

        return _wrapped_view

    return decorator
